vec2 fixuv(in vec2 uv) {

    vec2 p = 2. * (2. * uv - iResolution.xy) / min(iResolution.x, iResolution.y);
    return p;
}

vec3 drawGrid(in vec2 p) {

    vec3 col = vec3(0.45);
    float m = p.x - fract(p.x) + p.y - fract(p.y);
    col = mix(col, vec3(0.85), smoothstep(0., 1., mod(m, 2.)));
    col = mix(col, vec3(1., 0., 0.), smoothstep(abs(p.x), abs(p.x), fwidth(p.x)));
    col = mix(col, vec3(0., 1., 0.), smoothstep(abs(p.y), abs(p.y), fwidth(p.y)));
    return col;

}

//圆形距离场
float sdCircle(in vec2 p, in float r) {
    return length(p) - r;
}

// b.x = width
// b.y = height
// r.x = roundness top-right  
// r.y = roundness boottom-right
// r.z = roundness top-left
// r.w = roundness bottom-left
float sdRoundeBox(in vec2 p, in vec2 b, in vec4 r) {
    r.xy = (p.x > 0.) ? r.xy : r.zw;
    r.x = (p.y > 0.) ? r.x : r.y;
    vec2 q = abs(p) - b + r.x;
    return min(max(q.x, q.y), 0.0) + length(max(q, 0.)) - r.x;

}
//r.x = x
//r.y = y
//r.z =width
//r.w = height;
//r.x =left
//r.y = top
//r.x+r.z = right
//r.y +r.w = bottom
// float sdRoundeBox2(in vec2 p,in vec4 r){
//     r.xy = (p.x>0.)?r.xy:r.zw;
//     r.x = (p.y>0.)?r.x:r.y;
//     vec2 q = abs(p)-b+r.x;
//     return min(max(q.x,q.y),0.0)+length(max(q,0.))-r.x;

// }

float segment(in vec2 p, in vec2 sp, in vec2 ep, in float w) {

    float f = 0.;
    vec2 v1 = ep - sp;
    vec2 v2 = p - sp;
    vec2 u = normalize(v1);
    // // 求夹角
    // float theta = acos(dot(v1,u)/(length(v1)*length(u)));
    // float c = length(v2)/cos(theta);
    u = u * dot(v2, v1) / length(v1);

    // u = u*ud;
    float d = length(u - v2);

    // float e= sqrt(pow(length(v2),2.)-pow(ud,2.));

    f = smoothstep(w / 2., 0., d);
    return f;
}

bool equaldis(in vec2 p, in vec2 sp, in vec2 ep) {

    vec2 v1 = normalize(ep - sp);
    vec2 v2 = normalize(p - sp);
    return 0. <= dot(v1, v2);
}

bool posTriangleInner(in vec2 p, in vec2 p1, in vec2 p2, in vec2 p3) {

    // f += equaldis(p,p1,p2)?1.:-1.;
    // f += equaldis(p,p2,p1)?1.:-1.;
    // f += equaldis(p,p1,p3)?1.:-1.;
    // f += equaldis(p,p3,p2)?1.:-1.;
    // f += equaldis(p,p2,p3)?1.:-1.;
    // f += equaldis(p,p3,p2)?1.:-1.;

    return equaldis(p, p1, p2) && equaldis(p, p2, p1) && equaldis(p, p1, p3) && equaldis(p, p3, p2) &&
        equaldis(p, p2, p3) && equaldis(p, p3, p2);

}

float cros(in vec2 a, in vec2 b, in vec2 p) {
    return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
}

bool toLeft(in vec2 a, in vec2 b, in vec2 p) {
    return cros(a, b, p) > 0.;
}

bool inTriangle(in vec2 p, in vec2 a, in vec2 b, in vec2 c) {
    bool res = toLeft(a, b, p);
    if(res != toLeft(b, c, p))
        return false;
    if(res != toLeft(c, a, p))
        return false;
    if(cros(a, b, c) == 0.)    //ABC is in one line，不构成三角形
        return false;
    return true;
}

float drawTriangle(in vec2 p, in vec2 p1, in vec2 p2, in vec2 p3) {
    float f = 0.;
    if(inTriangle(p, p1, p2, p3)) {
        return 1.;
    }
    return f;
}

float isHitArea(in vec2 p, in vec2 lt, in vec2 rt, in vec2 lb, in vec2 rb) {

    float f = 0.;

    if(inTriangle(p, lt, rt, lb) ||
        inTriangle(p, lt, lb, rb) ||
        inTriangle(p, lt, rt, rb) ||
        inTriangle(p, rb, rt, lb)) {
        f = 1.;
    }

//  float a = (lt.x - lb.x) * (p.y - lb.y) - (lt.y - lb.y) * (p.x - lb.x);
//             float b = (rt.x - lt.x) * (p.y - lt.y) - (rt.y - lt.y) * (p.x - lt.x);
//             float c = (rb.x - rt.x) * (p.y - rt.y) - (rb.y - rt.y) * (p.x - rt.x);
//             float d = (lb.x - rb.x) * (p.y - rb.y) - (lb.y - rb.y) * (p.x - rb.x);
//     if ((a > 0. && b > 0. && c > 0. && d > 0.) || (a < 0. && b < 0. && c < 0. && d < 0.)){
//         f=1.;
//     }

    return f;
}

float segment3(in vec2 p, in vec2 sp, in vec2 ep, in float w) {
    float f = 0.;
    vec2 v1 = ep - sp;
    vec2 v2 = p - sp;
    float proj = clamp(dot(v1, v2) / pow(length(v1), 2.), 0., 1.);
    float d = length(proj * v1 - v2);
    if(d <= w) {
        f = 1.;
    }
    return f;
}

vec3 drawPoint(in vec2 p, in vec2 pos, in float s,in vec3 color, in vec3 col) {

    float f = 0.;

    float d = length(p - pos);
    f = smoothstep(s, s * 0.99, d);
    col = mix(col,color,f);
    return col;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {

    vec2 p = fixuv(fragCoord);
    float f = sdCircle(p, 0.5);
    vec3 col = drawGrid(p);
    // col  = mix(col,vec3(1.),sin( smoothstep(0.,1.,f)*sin(iTime+20.)*30.));
    // col = mix(col, vec3(1., 0.5, 0.3), 1. - smoothstep(0., 1. * 0.01, (sdRoundeBox(p - vec2(0.3, 0.2), vec2(.6, 0.5), vec4(2., 2, 2., 2.)))));
    // float s = segment3(p, vec2(0., 0.), vec2(-1., 2.), fwidth(p.x) * 1.);
    // float s1 = segment(p, vec2(0., 0.), vec2(-1., -2.), fwidth(p.x) * 10.);
    // col = mix(col, vec3(1., 1., 0.), s);
    // col = mix(col, vec3(1., 1., 0.), s1);

    vec2 lt = vec2(1., 2.);
    vec2 rt = vec2(0., 2.);
    vec2 lb = vec2(0., 0.);
    vec2 rb = vec2(1.0, 0.);

    

    col = mix(col, vec3(0.9, 1.0, 1.0), isHitArea(p, lt, rt, lb, rb));
   // vec2 p1 = vec2(0., 0.3);
    // vec2 p2 = vec2(0., 1.);
    // vec2 p3 = vec2(1., 0.);
    // col = mix(col, vec3(0.9, 1.0, 1.0), drawTriangle(p, p1, p2, p3));

    col = drawPoint(p,lt,fwidth(p.x)*5.,vec3(1.,0.,0.),col);
    col = drawPoint(p,rt,fwidth(p.x)*5.,vec3(1.,0.5,0.),col);
    col = drawPoint(p,lb,fwidth(p.x)*5.,vec3(0.12, 1, 0),col);
    col = drawPoint(p,rb,fwidth(p.x)*5.,vec3(1, 0, 0.5),col);

    fragColor = vec4(col, 1.);
}